<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		$this->load->helper('url');
		$this->load->library('tank_auth');
	}

	function _remap($method, $params = array())
	{
		redirect('/backend/main/');
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		} else {
			if(!$this->tank_auth->is_profile_set()){
				redirect('/auth/set_profile/');
			} else {
				$this->profiles=$this->tank_auth->get_profile();
				$isOwner=false;
				$isClient=false;
				foreach($this->profiles as $row){
					if($row->type==="Owner"){
						$isOwner=true;
					} else if($row->type==="Client"){
						$isClient=true;
					}
				}
				if ($isOwner && !$isClient){
					redirect('/backend/main/');
				} else if (!$isOwner && $isClient){
					redirect('/frontend/main/');
				} else if ( $isOwner && $isClient){
					$this->index();
				} else {
					redirect('/auth/logout/'); //Invalid user profile.
				}
			}
			
		}
		show_404(); // Either means that the user isn't authorized or that the page doesn't exist.
	}

	function index()
	{
		$data['user_id']	= $this->tank_auth->get_user_id();
		$data['username']	= $this->tank_auth->get_username();
		$data['profiles']	= $this->profiles;
		$this->load->view('welcome', $data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
